/*
 Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
 Original sources are available at www.latestbit.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 TCPSharedResources.hpp. Created on: 19.08.2009 12:53:00
 */

#ifndef TCPSHAREDRESOURCES_HPP_
#define TCPSHAREDRESOURCES_HPP_

#include "SocketMultiplexer.hpp"
#include "smartobjects/SmartObjectsDefs.hpp"
#include "smartobjects/network/IHandshakeManagerFactory.hpp"
#include "smartobjects/network/IPacketFormerFactory.hpp"


namespace smartobjects {

class TCPSharedResources {
public:
	typedef boost::shared_ptr<TCPSharedResources> SharedPtr;

public:
	SO_DLLPRFX TCPSharedResources();
	SO_DLLPRFX ~TCPSharedResources();

	SO_DLLPRFX IPacketFormerFactory* getPacketFormerFactory();
	SO_DLLPRFX void setPacketFormerFactory(IPacketFormerFactory*);

	SO_DLLPRFX IHandshakeManagerFactory* getHandshakeManagerFactory();
	SO_DLLPRFX void setHandshakeManagerFactory(IHandshakeManagerFactory*);

	SO_DLLPRFX ThreadPool::SharedPtr getThreadPool();
	SO_DLLPRFX ThreadPool::SharedPtr getThreadPool(const std::string& sGroupName);

	SO_DLLPRFX Timer::SharedPtr getHandshakeTimer();

	SO_DLLPRFX smartobjects::sockets::SocketMultiplexer::SharedPtr getSocketMultiplexer();
	SO_DLLPRFX smartobjects::sockets::SocketMultiplexer::SharedPtr getSocketMultiplexer(const std::string& sGroupName);

	SO_DLLPRFX cpputils::utils::SequenceGenerator<long>& getSequenceGenerator();

private:
	SO_DLLPRFX static const char* 			DEFAULT_GROUP_NAME;
	SO_DLLPRFX static const unsigned int 	DEFAULT_INIT_THREADS_CNT;
	SO_DLLPRFX static const unsigned int 	DEFAULT_MAX_THREADS_CNT;
	SO_DLLPRFX static const char* 			TCP_SHARED_THREAD_POOL_ID;
	SO_DLLPRFX static const char* 			TCP_SHARED_HANDSHAKE_TIMER_ID;

private:
	IHandshakeManagerFactory* m_poHandshakeManagerFactory;
	IPacketFormerFactory* m_poPacketFormerFactory;

	typedef std::map< std::string, ThreadPool::SharedPtr > ThreadPools;
	ThreadPools m_mThreadPools;
	typedef std::map< std::string, smartobjects::sockets::SocketMultiplexer::SharedPtr > SocketMultiplexers;
	SocketMultiplexers m_mSocketMultiplexers;

	boost::recursive_mutex m_mxResources;

	cpputils::ept::Timer::SharedPtr m_poHandshakeTimer;
	cpputils::utils::SequenceGenerator<long> m_oSequenceGenerator;
};

}

#endif /* TCPSHAREDRESOURCES_HPP_ */
